package cn.credit.loan.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.credit.loan.entity.Customer;
import cn.credit.loan.service.customer.CustomerService;

@RestController
public class CustomerController {
	private static Logger logger = Logger.getLogger(CustomerController.class);
	// 注入
	@Autowired
	private CustomerService customerService;

	/**
	 * 查看用户基本信息
	 * 
	 * @param customer
	 * @param pageindex
	 * @return
	 */
	@RequestMapping(value = "/cutoList")
	public ModelAndView CustList(Customer customer,@RequestParam(value = "pageIndex", defaultValue = "1", required = false) Integer pageIndex) {
		ModelAndView modelAndView = new ModelAndView();
		logger.info("####------->> 进入查询用户基本信息列表");
		PageHelper.startPage(pageIndex, 10);
		List<Customer> custlist = null;
		try {
			custlist = customerService.getCustomerList(customer);
			PageInfo<Customer> pageInfo = new PageInfo<Customer>(custlist);
			modelAndView.addObject("custlist", custlist);
			modelAndView.addObject("customer", customer);
			modelAndView.addObject("pageInfo", pageInfo);
			List<Customer> cust = customerService.getCustomerList(null);
			modelAndView.addObject("cust", cust);
			modelAndView.setViewName("after/customerlist");
		} catch (Exception e) {
			logger.info("####----->> 出现异常，请检查");
			e.printStackTrace();
		}
		return modelAndView;
	}

	/**
	 * 添加客户基本信息（前台）
	 * 
	 * @return
	 */ 
	@RequestMapping(value = "/addcustomer",method=RequestMethod.POST)
	@ResponseBody
	public Object addCust(Customer customer,@RequestParam(value="age",required=true)String Age) {
		Map<String, Object> result = new HashMap<String, Object>();
		logger.info("####------>> 录入用户基本信息");
		try {
			Customer omer=customerService.getcustomer(customer);
			if(omer==null){
				if(Age!=null){
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
					Date date = sdf.parse(Age);
					customer.setCustBirthAge(date);
					logger.info("####------>> 成功！");
					result.put("add", customerService.addCustomer(customer));
				}else{
					logger.info("####------>> 出生年月不能为空");
				}
			}else{
				result.put("add", "exist");
			}
		} catch (Exception e) {
			logger.info("####----->> 出现异常");
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 修改个人基本信息
	 * @param customer
	 * @return
	 */
	@RequestMapping(value = "/upcustomer")
	public Object updateCust(Customer customer) {
		Map<String, Object> result = new HashMap<String, Object>();
		logger.info("####------->> 进入修改用户录入的基本信息");
		try {
			if (customerService.updateCustomer(customer)) {
				result.put("updated", "treu");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return JSONArray.toJSONString(result);
	}

	/**
	 * 根据客户id查询客户的信息
	 * 
	 * @param custId
	 * @return
	 */
	@RequestMapping(value = "/getCustId")
	@ResponseBody
	public Object CustListById(@RequestParam(value = "custId", required = false) Integer custId) {
		Customer custIdlist = null;
		Map<String, Object> map = new HashMap<String, Object>();
		logger.info("####------->>查询用户的基本信息");
		try {
			custIdlist = customerService.getCustomerListById(custId);
			map.put("custIdlist", custIdlist);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return JSONArray.toJSONString(map);
	}
}
